/*************************************************
 *FileName:      appointment.js
 *Description:   我的约会.js
 *Others:        陈灿
 *************************************************/
    /*
     * 启动
     */
    mui.init();
    mui('.mui-scroll-wrapper').scroll({
        indicators: false,      //是否显示滚动条 默认为true
        deceleration: 0.003,    //阻尼系数,系数越小滑动越灵敏 默认0.0006
        bounce: true           //是否启用回弹 默认true
    });
    /*
     * 变量初始化
     */
    var uid = localStorage.replace_uid;
    var token = localStorage.replace_token;
    var negative,praise;
    /*
     * 主程序
     */ 
     mui.init({
        beforeback: function() {
            var list = plus.webview.currentWebview().opener();
            mui.fire(list, 'refreshindex');
            return true;
        }
    });
    $(document).ready(function() 
    {
        //样式加载
        pagecss();
        //收到的约会
    });
    /**
     * pagecss
     * 样式加载
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function pagecss(){
        mui.plusReady(appload)
        mui.plusReady(apppost)
        mui.plusReady(negatives)
//      negatives()
//      apppost()
//      appload()
        $('.appointment').on('tap','.agree',function(){//点击同意显示已同意
            if(!$(this).parents('.receive-box').hasClass('lose-box')&&!$(this).parents('.receive-box').hasClass('agree-box')){
                var ouser =$(this).parents('.receive-box').find('h3').text();
                var that =$(this);
                mui.confirm('确定同意与'+ouser+'的约会？','<div class="promit-msg">提示</div>',['确认','我再想想'],function(e){ 
                    if(e.index==false){
                        agreedate(that);
                    }
                },'div')
            }
        })
        $('.appointment ').on('tap','.cancel',function(){
            if(!$(this).parents('.receive-box').hasClass('lose-box')){//已失效之后按钮就不能点击
                var ouser =$(this).parents('.receive-box').find('h3').text();
                var obox=$(this).parents('.receive-box');
                mui.confirm('你是否取消与'+ouser+'的约会？','<div class="promit-msg">提示</div>',['确认','取消'],function(e){ 
                    if(!e.index){
                        canceldate(obox);
                    }
                },'div')
            }
        })
        
        $('.appointment ').on('tap','.addfriends',function(){
            addfriendsmsg($(this).parents('.receive-box').attr('data-uid'));
            if($('.pitch').index()){
                
//              var ourl ='/index.php/api/users_date/myDateDel';
//              addfriends(obox,ourl);
            }else{
//              var ourl ='/index.php/api/users_date/dateMeDel';
//              addfriends(obox,ourl);
            }
        })
        
        $('.appointment').on('tap','.content-img',function(){
            var oguid = $(this).parents('.receive-box').attr('data-uid');
            common.pageTransition('personal.html?friend_uid='+oguid)
        })//选项卡
        
        $('.appointment').on('swipeleft','.roll',function(){
            if($(this).parents('.receive-box').hasClass('lose-box')){
                $(this).css({marginLeft: '-40%'})
            }
        })
        
        $('.appointment').on('swiperight','.roll',function(){
            $(this).css({marginLeft: '0'})
        })
        
        $('.appointment').on('tap','.agree-box .lose-img',function(){
            var ofuid = $(this).parents('.receive-box').attr('data-uid');
            var dataid = $(this).parents('.receive-box').attr('data-id');
            common.pageTransition('chat.html?thatid='+ofuid+'&&state='+dataid)
        })
        
        $('.appointment').on('tap','.delect',function(){
            var obox=$(this).parents('.receive-box');
            if($('.pitch').index()){
                var ourl ='/index.php/api/users_date/myDateDel';
                delectdate(obox,ourl);
            }else{
                var ourl ='/index.php/api/users_date/dateMeDel';
                delectdate(obox,ourl);
            }
        })
        
        $('.appointment').on('tap','.complaint',function(){
            var ouid=$(this).parents('.receive-box').attr('data-uid');
            var orid=$(this).parents('.receive-box').attr('data-id');
            common.pageTransition('complaint.html?thatid='+ouid+'&&orid='+orid);
        })
        
        $('.appointment').on('tap','.tab a',function(){
            $(this).addClass('pitch').siblings().removeClass('pitch');
            $('.mui-scroll-wrapper').eq($(this).index()).css({display:'block'}).siblings('.mui-scroll-wrapper').css({display:'none'});
        })
        
        $('.appointment').on('tap','ul li em',function(){//点击同意显示已同意
            $(this).parents('li').find('em').removeClass('active');
            $(this).parents('li').find('i').removeClass('active');
            $(this).addClass('active').prevAll().addClass('active');
            oshu();
        })
        
        $('.appointment').on('tap','ul li i',function(){//点击同意显示已同意
            $(this).parents('li').find('em').removeClass('active');
            $(this).parents('li').find('i').removeClass('active');
            $(this).addClass('active').prevAll().addClass('active');
            oshu();
        })
        
        function oshu(){
            var osub=0;
            $('.evaluate-box ul li').each(function(){
                var ot = $(this).find('.active').parent().index();
                if(ot==1){
                    osub+=praise*($(this).find('.active').length)
                }else if(ot==2){
                    osub-=negative*($(this).find('.active').length)
                }
            })
            if(osub<0){
                $('.evaluate-box .sex').html('对方可扣除魅力值：<span class="color">'+osub+'</span>点')
            }else{
                $('.evaluate-box .sex').html('对方可获得魅力值：<span class="color">'+osub+'</span>点')
            }
        }
        
        $('.appointment').on('tap','.agree-box .evaluate',function(){//评价
            var pards =$(this).parents('.roll');
            if(pards.find('.evaluate-box').length==0) {
            pards.append('<div class="evaluate-box"><p class="title"><span class="title-span">好评</span><span class="title-span">差评</span></p><ul><li><span class="text">照片是否真实</span><span><i></i><i></i><i></i><i></i><i></i></span><span><em></em><em></em><em></em><em></em><em></em></span></li><li><span class="text">约会是否开心</span><span><i></i><i></i><i></i><i></i><i></i></span><span><em></em><em></em><em></em><em></em><em></em></span></li><li><span class="text">约会对象满意度</span><span><i></i><i></i><i></i><i></i><i></i></span><span><em></em><em></em><em></em><em></em><em></em></span></li><li><span class="text">约会对象是否准时</span><span><i></i><i></i><i></i><i></i><i></i></span><span><em></em><em></em><em></em><em></em><em></em></span></li><li><span class="text">约会对象诚意度</span><span><i></i><i></i><i></i><i></i><i></i></span><span><em></em><em></em><em></em><em></em><em></em></span></li></ul><p class="sex"></p><p class="rdntn-p"><button class="rdntn">确认提交</button></p></div>');
            }else{
                pards.find('.evaluate-box').remove();
            }
        })
        
        $('.appointment').on('tap','.rdntn',function(){
            var arr={};
            var othat =$(this);
            $('li').each(function(){
                var ozhen =$(this).find('.active').parent().index();
                var oid =$(this).find('.active').length;
                if($(this).find('.active').length==0){
                    mui.toast('请填写完整评价！');
                    return false;
                }
                ozhen==1 ? false:oid=-oid;
                arr[$(this).index()]=oid;
            })
            postlist(othat,arr);
        })
    }
    //我的约会数值
    function negatives(){
        $.ajax({
            type: "post",
            url: DOMAIN_NAME + "/index.php/api/users_date/dateCommentExpSet",
            dataType: 'jsonp',
            data: {},
            success: function(data) {
                if(data.error_code == 0) {
                    negative=data.data.negative;
                    praise=data.data.praise;
                } else if(data.error_code == 1001) {
                    setTimeout(function() {
                        exitlogin();
                    },1000);
                } else {
                }
            }
        });
    }

//收到的约会
function appload() {
    $.ajax({
        type: "post",
        url: DOMAIN_NAME + "/index.php/api/users_date/datemelist",
        dataType: 'jsonp',
        data: {
            uid: uid,
            token: token,
        },
        success: function(data) {
            if(data.error_code == 0) {
                var oload='';
                for(var i=0 ; i<data.data.length ; i++){
                    var time = data.data[i].create_time;  
                    var date = new Date(time * 1000);
                    var year = date.getFullYear();  
                    var mou = date.getMonth()+1;  
                    mou<10 ? mou="0"+mou : null;
                    var day = date.getDate();
                    day<10 ? day="0"+day : null;
                    var otime = year+'-'+mou+'-'+day;
                    var status ,status2='同意约会';
                    if(data.data[i].status==0 ){
                        status='' ;
                    }else if(data.data[i].status==1){
                        status='agree-box';
                        status2='已同意';
                    }else if(data.data[i].status==2){
                        status='lose-box';
                    }else{
                        status='lose-box';
                        status2='已同意';
                    }
                    oload+='<div class="receive-box '+status+'" data-id="'+data.data[i].id+'" data-uid="'+data.data[i].uid+'"><div class="roll"><div class="content"><div class="content-img">'
                            +'<img onerror="imgExists(this)" src="'+DOMAIN_NAME+data.data[i].face+'"/>'
                            +'</div><div class="content-right"><h3>'+data.data[i].nickname+'</h3>'
                            +'<p>'+otime+'</p></div><div class="lose-img"></div></div><div class="btn">'
                            +'<a class="agree">'+status2+'</a><a class="cancel">取消约会</a>'
                            +'<a class="addfriends">加好友</a></div><em class="mui-icon mui-icon-arrowright"></em><i class="delect"></i><i class="complaint"></i></div> </div>';
                }
                $('.mui-scroll-wrapper').eq(0).find('.mui-scroll').html(oload);
                               
            } else if(data.error_code == 1001) {
                setTimeout(function() {
                    exitlogin();
                },1000);
            } else {
            }
        }
    });
}


//发起的约会
function apppost() {
    $.ajax({
        type: "post",
        url: DOMAIN_NAME + "/index.php/api/users_date/mydatelist",
        dataType: 'jsonp',
        data: {
            uid: uid,
            token: token,
        },
        success: function(data) {
            if(data.error_code == 0) {
                var opost='';
                for(var i=0 ; i<data.data.length ; i++){
                    var time = data.data[i].create_time;  
                    var date = new Date(time * 1000);
                    var year = date.getFullYear();  
                    var mou = date.getMonth()+1;  
                    mou<10 ? mou="0"+mou : null;
                    var day = date.getDate();
                    day<10 ? day="0"+day : null;
                    var otime = year+'-'+mou+'-'+day;
                    var status ,status2='<a class="cancel">取消约会</a><a class="evaluate" >评价</a>';
                    if(data.data[i].status==0 ){
                        status='' ;
                    }else if(data.data[i].status==1){
                        status='agree-box';
                        status2='<a class="agree">已同意</a><a class="cancel">取消约会</a><a class="evaluate" >评价</a><a class="addfriends">加好友</a>';
                    }else if(data.data[i].status==2){
                        status='lose-box';
                    }else{
                        status='lose-box';
                        status2='<a class="agree">已同意</a><a class="cancel">取消约会</a><a class="evaluate" >评价</a><a class="addfriends">加好友</a>';
                    }
                    opost+='<div class="receive-box '+status+'" data-id="'+data.data[i].id+'" data-uid="'+data.data[i].friend_uid+'"><div class="roll"><div class="content"><div class="content-img">'
                            +'<img onerror="imgExists(this)" src="'+DOMAIN_NAME+data.data[i].face+'"/>'
                            +'</div><div class="content-right"><h3>你对'+data.data[i].nickname+'发起了约会</h3>'
                            +'<p>'+otime+'</p></div><div class="lose-img"></div></div><div class="btn">'
                            +status2+'</div><em class="mui-icon mui-icon-arrowright"></em><i class="delect"></i><i class="complaint"></i></div></div>';
                }
                $('.mui-scroll-wrapper').eq(1).find('.mui-scroll').html(opost);
            } else if(data.error_code == 1001) {
                setTimeout(function() {
                    exitlogin();
                },1000);
            } else {
            }
        }
    });
}


//同意约会
function agreedate(obj) {
    var id = obj.parents('.receive-box').attr('data-id');
    $.ajax({
        type: "post",
        url: DOMAIN_NAME + "/index.php/api/users_date/acceptDate",
        dataType: 'jsonp',
        data: {
            uid: uid,
            token: token,
            id:id,
        },
        success: function(data) {
            if(data.error_code == 0) {
                obj.addClass('actice');
                obj.text('已同意');
                obj.parents('.receive-box').addClass('agree-box');
            } else if(data.error_code == 1001) {
                setTimeout(function() {
                    exitlogin();
                },1000);
            } else {
                mui.toast(data.error_msg)
            }
        }
    });
}



//取消约会
function canceldate(obj) {
    var id = obj.attr('data-id');
    $.ajax({
        type: "post",
        url: DOMAIN_NAME + "/index.php/api/users_date/refuseDate",
        dataType: 'jsonp',
        data: {
            uid: uid,
            token: token,
            id:id,
        },
        success: function(data) {
            if(data.error_code == 0) {
                if($('.pitch').index()==1){
                    obj.find('.agree').remove();
                    obj.find('.addfriends').remove();
                }else{
                    obj.find('.agree').text('同意约会');
                }
                obj.addClass('lose-box').removeClass('agree-box');//收到约会里取消约会
            } else if(data.error_code == 1001) {
                setTimeout(function() {
                    exitlogin();
                },1000);
            } else {
                mui.toast('拒绝失败')
            }
        }
    });
}


//删除记录
function delectdate(obj,ourl) {
    var id = obj.attr('data-id');
    $.ajax({
        type: "post",
        url: DOMAIN_NAME + ourl,
        dataType: 'jsonp',
        data: {
            uid: uid,
            token: token,
            id:id,
        },
        success: function(data) {
            if(data.error_code == 0) {
                obj.remove();
            } else if(data.error_code == 1001) {
                setTimeout(function() {
                    exitlogin();
                },1000);
            } else {
                mui.toast('删除失败')
            }
        }
    });
}


//上传评价
function postlist(obj,arr) {
    var id = obj.parents('.receive-box').attr('data-id');
    var real_photo = arr[0],happy_dating = arr[1] ,satisfied = arr[2] ,punctual = arr[3] ,sincerity = arr[4] ;
    $.ajax({
        type: "post",
        url: DOMAIN_NAME + "/index.php/api/users_date/dateComment",
        dataType: 'jsonp',
        data: {
            uid: uid,
            token: token,
            id:id,
            real_photo:real_photo,
            happy_dating:happy_dating,
            satisfied:satisfied,
            punctual:punctual,
            sincerity:sincerity,
        },
        success: function(data) {
            if(data.error_code == 0) {
                obj.parents('.receive-box').removeClass('agree-box').addClass('lose-box');
                obj.parents('.evaluate-box').remove();
                mui.toast('评价成功!');
            } else if(data.error_code == 1001) {
                setTimeout(function() {
                    exitlogin();
                },1000);
            } else {
                mui.toast(data.error_msg)
            }
        }
    });
}



//加好友需要的条件
function addfriendsmsg(obj) {
    $.ajax({
        type: "post",
        url: DOMAIN_NAME + "/index.php/api/user/applyNeed",
        dataType: 'jsonp',
        data: {
            uid: uid,
            token: token,
            friend_uid:obj,
        },
        success: function(data) {
            if(data.error_code == 0) {
                function ohuanbi(){
                    mui.confirm('加该好友需要换币：<em class="gift-jb"></em> ×'+data.data.amount+'，<br/>是否支付？','提示',['支付','取消'],function(e){
                        if(e.index == 0){
                            if(data.data.balance<data.data.amount){
                                mui.confirm('您当前换币剩余'+data.data.balance+'个<br/>余额不足，请充值','提示',['支付','取消'],function(e){
                                    if(e.index == 0){
                                    }
                                },'div');
                            }else{
                                addfriends(obj,'balance');
                            }
                        }
                    },'div');
                }
                if(data.data.cards>0){
                    mui.confirm('您有好友卡：<em class="gift-hyk"></em> ×'+data.data.cards+'，<br/>是否使用？','提示',['使用','取消'],function(e){
                        if(e.index == 0){
                            addfriends(obj,'cards');
                        }else{
                            ohuanbi();
                        }
                    },'div');
                }else{
                    ohuanbi();
                }
                
            } else if(data.error_code == 1001) {
                setTimeout(function() {
                    exitlogin();
                },1000);
            } else {
                mui.toast(data.error_msg);
            }
        }
    });
}


//加好友
function addfriends(obj,method) {
    $.ajax({
        type: "post",
        url: DOMAIN_NAME + "/index.php/api/friends_apply/friendsApplyAdd",
        dataType: 'jsonp',
        data: {
            uid: uid,
            token: token,
            friend_uid:obj,
            method:method,
        },
        success: function(data) {
            if(data.error_code == 0) {
                mui.toast('申请成功！');
                
            } else if(data.error_code == 1001) {
                setTimeout(function() {
                    exitlogin();
                },1000);
            } else {
                mui.toast(data.error_msg);
            }
        }
    });
}